package com.ziyueyuwen.xiniao.controller.curve;

import com.ziyueyuwen.xiniao.bean.vo.ZyUserVO;
import com.ziyueyuwen.xiniao.dao.ZyUserMapper;
import com.ziyueyuwen.xiniao.util.CalendarUtil;
import com.ziyueyuwen.xiniao.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wancheng  on 2019/3/8.
 */
@Controller
@RequestMapping("/statistics")
public class CurveController {

    @Autowired
    ZyUserMapper userMapper;

    @RequestMapping(value = "/curve", method = RequestMethod.GET)
    public String curve(Model model) {
        List<ZyUserVO> data = userMapper.getStudentCreateCountGroupByDay();
        Map<Date,Integer> countMap = data.stream().collect(Collectors.toMap(ZyUserVO::getCreateTime,ZyUserVO::getCount));
        List<Date> betweenDates = CalendarUtil.getBetween(DateUtil.str2Date("2019-01-19 00:00:00"),new Date());
        Map<Date,Integer> dateMap = new HashMap<>(365);
        betweenDates.forEach(
            d->{
                if (countMap.get(d) == null){
                    dateMap.put(d,0);
                }else {
                    dateMap.put(d,countMap.get(d));
                }
            }
        );
        List<Integer> res = new ArrayList<>();
        dateMap.entrySet().forEach(e->res.add(e.getValue()));
        model.addAttribute("data",res);
        return "/statistics/curve";
    }
}
